﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BUS;
using DTO;
using System.IO;
using System.Data.Odbc;
using DevExpress.XtraReports.UI;

namespace GUI
{
    public partial class FormBaocaodiemthanhvien : Window
    {
        DataTable tb=new DataTable();
        public FormBaocaodiemthanhvien()
        {
            InitializeComponent();
            hoiThoat = false;
        }
        private void laydulieuBCDT()
        {

            string chuoi = cbbThangNam.Text;
            var ngay = chuoi.Split('/');
            var st = new DateTime(int.Parse(ngay[1]), int.Parse(ngay[0]), 1);
            OleDbDataAdapter da = BUS.BaoCaoDiemBUS.BCDiemTV(st);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.Columns.Clear();
            tb = ds.Tables[0].Copy();
            //dataGridView1.DataSource = ds.Tables[0];
            List<SanPhamDTO> dssp = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            dssp = bus.LayDSSP();
            for (int i = 0; i <= dssp.Count - 1; i++)
            {
                DataColumn cl = new DataColumn(dssp[i].MaSP.ToString());
                tb.Columns.Add(cl);
            }
            DataColumn cv = new DataColumn("DiemCV");
            tb.Columns.Add(cv);
            dataGridView1.DataSource = tb.DefaultView;
            for (int i = 0; i <= dssp.Count - 1; i++)
            {
                dataGridView1.Columns[i + 2].HeaderText = dssp[i].TenSP;

            }
            dataGridView1.Columns[0].HeaderText = "Mã NV";
            dataGridView1.Columns[1].HeaderText = "Họ Tên";
            dataGridView1.Columns[dataGridView1.ColumnCount - 1].HeaderText = "Điểm CV";
            for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                int manv = int.Parse(dataGridView1[0, i].Value.ToString());
                int diemcv = BUS.BaoCaoDiemBUS.TongCVTheoTV(manv, st);
                tb.Rows[i][tb.Columns.Count - 1] = diemcv;
                for (int j = 0; j <= dssp.Count - 1; j++)
                {
                    int sl = BaoCaoDiemBUS.TongSPTheoTV(dssp[j].MaSP, manv, st);                    
                    int tongsp = dssp.Count;
                    tb.Rows[i][j + 2] = sl;
                }
            }

        }
        private void LayBCDiemChiTietTV()
        {
            string chuoi = cbbThangNam.Text;
            var ngay = chuoi.Split('/');
            var st = new DateTime(int.Parse(ngay[1]), int.Parse(ngay[0]), 1);
            int manv = int.Parse(txtMaNV.Value.ToString());
            DataTable x = BUS.BaoCaoDiemBUS.DiemChiTietTV(manv,st);
            tb = x.Copy();            
            List<SanPhamDTO> dssp = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            dssp = bus.LayDSSP();
            for (int i = 0; i <= dssp.Count - 1; i++)
            {
                
                DataColumn cl = new DataColumn(dssp[i].TenSP.ToString());
                tb.Columns.Add(cl);
            }
            dataGridView1.Columns.Clear();
            dataGridView1.DataSource = tb.DefaultView;
            for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                int mahd = int.Parse(dataGridView1[0, i].Value.ToString());
                int masp = int.Parse(dataGridView1[3, i].Value.ToString());                
                for (int j = 0; j <= dssp.Count - 1; j++)
                {
                    if (dssp[j].MaSP==masp)
                    {
                        int sl = BaoCaoDiemBUS.TongSPChiTietTheoTV(masp, manv,mahd, st);
                        tb.Rows[i][j + 4] = sl;
                    }                  
                    
                }
            }
            dataGridView1.Columns[0].HeaderText = "Mã HĐ";
            dataGridView1.Columns[1].HeaderText = "Ngày HĐ";
            dataGridView1.Columns[2].HeaderText = "Điểm CV";
            dataGridView1.Columns[3].Visible = false;
            
        }
        private void LaydulieuTungTV()
        {
            LayBCDiemChiTietTV();
        }
        private void FormBaocaodiemthanhvien_Load(object sender, EventArgs e)
        {
            txtHoTen.Visible = false;
            txtMaNV.Visible = false;
            DataTable tb = BUS.NhanVienBUS.LayMaVaTen();            
            txtMaNV.DataSource = tb;
            for (int i = 1; i <= 12; i++)
            {
                DateTime st = new DateTime(DateTime.Now.Year, i, 1);
                string ng = st.ToString("MM/yyyy");
                this.cbbThangNam.Items.Add(ng);
                st = st.AddMonths(i);
            }
            cbbThangNam.SelectedItem = cbbThangNam.Items[DateTime.Now.Month - 1];
            rbtALL.Checked = true;
            txtMaNV.Enabled=false;
            laydulieuBCDT();
        }     

        private void btnThoat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXuat_Click(object sender, EventArgs e)
        {
            XuatExcel(dataGridView1, "BaoCaoDiemTV");
        }

        private void rbtNV_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNV.Enabled = true;
            txtMaNV.Visible = true;
            dataGridView1.Columns.Clear();
            txtHoTen.Visible = false;
            txtMaNV.Focus();
            
        }

        private void rbtALL_CheckedChanged(object sender, EventArgs e)
        {
            txtMaNV.Visible = false;
            dataGridView1.Columns.Clear();
            txtMaNV.Enabled = false;
            txtHoTen.Visible = false;
            cbbThangNam.Focus();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (rbtALL.Checked==true)
            {
                laydulieuBCDT();
            } 
            else
            {
                LaydulieuTungTV();
            }
        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {
            if (rbtALL.Checked==true)
            {
                try
                {
                    string filename = "baocaodiem.repx";                
                    string repXFile = System.Windows.Forms.Application.StartupPath + "\\Report\\" + filename;
                    XtraReport report = new XtraReport();
                    if (System.IO.File.Exists(repXFile))
                        report.LoadLayout(repXFile);
                    else
                        if (MessageBox.Show("Mẫu báo cáo không tồn tại có muốn tạo mói không?" , "QLKS", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            return;
                    DataSet ds1 = new DataSet();
                    ds1.Tables.Add(tb.Copy());
                    report.DataSource = ds1;
                    report.DisplayName = repXFile;
                    report.Name = filename;
                    DataTable tbheader = new DataTable();
                    foreach (DataGridViewColumn cl in dataGridView1.Columns)
                    {
                        DataColumn c = new DataColumn(cl.Name);
                        c.DefaultValue = cl.HeaderText;
                        tbheader.Columns.Add(c);
                    }
                    DataRow r = tbheader.NewRow();
                    tbheader.Rows.Add(r);
                    ds1.Tables.Add(tbheader);
                    //Parameter item = new Parameter();
                    //item.Name = "lblten";
                    //item.Value = 
                    //report.Parameters.Add(
                    report.ShowDesigner();//UserLookAndFeel.Default, DesignDockPanelType.FieldList | DesignDockPanelType.PropertyGrid);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } 
            else
            {
                try
                {
                    string filename = "baocaodiemchitiet.repx";
                    string repXFile = System.Windows.Forms.Application.StartupPath + "\\Report\\" + filename;
                    XtraReport report = new XtraReport();
                    if (System.IO.File.Exists(repXFile))
                        report.LoadLayout(repXFile);
                    else
                        if (MessageBox.Show("Mẫu báo cáo không tồn tại có muốn tạo mói không?", "QLKS", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            return;
                    DataSet ds1 = new DataSet();
                    ds1.Tables.Add(tb.Copy());
                    report.DataSource = ds1;
                    report.DisplayName = repXFile;
                    report.Name = filename;
                    DataTable tbheader = new DataTable();
                    foreach (DataGridViewColumn cl in dataGridView1.Columns)
                    {
                        DataColumn c = new DataColumn(cl.Name);
                        c.DefaultValue = cl.HeaderText;
                        tbheader.Columns.Add(c);
                    }
                    DataRow r = tbheader.NewRow();
                    tbheader.Rows.Add(r);
                    ds1.Tables.Add(tbheader);
                    //Parameter item = new Parameter();
                    //item.Name = "lblten";
                    //item.Value = 
                    //report.Parameters.Add(
                    report.ShowDesigner();//UserLookAndFeel.Default, DesignDockPanelType.FieldList | DesignDockPanelType.PropertyGrid);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
        }

        private void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (rbtALL.Checked==true)
            {
                try
                {
                    string filename = "baocaodiem.repx";               
                    string repXFile = System.Windows.Forms.Application.StartupPath + "\\Report\\" + filename;
                    XtraReport report = new XtraReport();

                    if (System.IO.File.Exists(repXFile))
                        report.LoadLayout(repXFile);
                    else
                    {
                        MessageBox.Show("Mẫu báo cáo không tồn tại!" , "QLKS", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        return;
                    }

                    DataSet ds1 = new DataSet();
                    ds1.Tables.Add(tb.Copy());
                    DataTable tbheader = new DataTable();
                    foreach (DataGridViewColumn cl in dataGridView1.Columns)
                    {
                        DataColumn c = new DataColumn(cl.Name);
                        c.DefaultValue = cl.HeaderText;
                        tbheader.Columns.Add(c);
                    }
                    DataRow r = tbheader.NewRow();
                    tbheader.Rows.Add(r);
                    ds1.Tables.Add(tbheader);
                    report.DataSource = ds1;
                    report.ShowPreview();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } 
            else
            {
                try
                {
                    string filename = "baocaodiemchitiet.repx";
                    string repXFile = System.Windows.Forms.Application.StartupPath + "\\Report\\" + filename;
                    XtraReport report = new XtraReport();

                    if (System.IO.File.Exists(repXFile))
                        report.LoadLayout(repXFile);
                    else
                    {
                        MessageBox.Show("Mẫu báo cáo không tồn tại!", "QLKS", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        return;
                    }

                    DataSet ds1 = new DataSet();
                    ds1.Tables.Add(tb.Copy());
                    DataTable tbheader = new DataTable();
                    foreach (DataGridViewColumn cl in dataGridView1.Columns)
                    {
                        DataColumn c = new DataColumn(cl.Name);
                        c.DefaultValue = cl.HeaderText;
                        tbheader.Columns.Add(c);
                    }
                    DataRow r = tbheader.NewRow();
                    tbheader.Rows.Add(r);
                    ds1.Tables.Add(tbheader);
                    report.DataSource = ds1;
                    report.ShowPreview();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
        }

        private void txtMaNV_Leave(object sender, EventArgs e)
        {
            object obj = txtMaNV.Value;           
            if (obj != null)
            {
                int manv = Convert.ToInt32(txtMaNV.Value);
                txtHoTen.Text = BUS.NhanVienBUS.LayTenNV(manv).ToString();
                txtHoTen.Visible = true;                
            }
            else
            {
                MessageBox.Show("Chưa chọn nhân viên!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rbtNV.Checked = true;
                return;                
            }
            
        }

        private void txtMaNV_Click(object sender, EventArgs e)
        {
            txtHoTen.Visible = false;
        }

        

        
        
                
    }
}
